package flc.ast.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.picselect.entity.SelectMediaEntity;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.databinding.DialogEditStyleBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.utils.TimeUtil;

public class EditDialog extends BaseSmartDialog<DialogEditStyleBinding> implements View.OnClickListener {
    public String hint;
    public SelectMediaEntity selectMediaEntity;
    private boolean isCollect = false;


    public EditDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_edit_style;
    }

    @Override
    protected void initView(View view) {
        if (!TextUtils.isEmpty(hint)) {
            mDataBinding.tvFolder.setText(hint);
        }
        Glide.with(getContext()).load(selectMediaEntity.getPath()).into((ImageView) mDataBinding.ivVideoImg);
        mDataBinding.tvVideoName.setText(selectMediaEntity.getMediaName());
        mDataBinding.tvVideoSize.setText(ConvertUtils.byte2FitMemorySize(selectMediaEntity.getSize(), 0));
        mDataBinding.tvVideoDuration.setText(TimeUtil.getMmss(selectMediaEntity.getDuration()));

        mDataBinding.ivVideoCollect.setOnClickListener(this);
        mDataBinding.tvRename.setOnClickListener(this);
        mDataBinding.tvShare.setOnClickListener(this);
        mDataBinding.tvFolder.setOnClickListener(this);
        mDataBinding.tvInfo.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);

        List<SelectMediaEntity> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            for (SelectMediaEntity mediaEntity : listSp) {
                if (mediaEntity.equals(selectMediaEntity)) {
                    isCollect = true;
                    break;
                }
            }
        }
        isCollect();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivVideoCollect://收藏
                List<SelectMediaEntity> listSp = AppSpUtils.getCollectList();
                if (isCollect) {
                    listSp.remove(selectMediaEntity);
                    AppSpUtils.saveCollectList(listSp);
                    isCollect = false;
                } else {
                    if (listSp != null && listSp.size() > 0) {
                        listSp.add(selectMediaEntity);
                        AppSpUtils.saveCollectList(listSp);
                    } else {
                        List<SelectMediaEntity> listSave = new ArrayList<>();
                        listSave.add(selectMediaEntity);
                        AppSpUtils.saveCollectList(listSave);
                    }
                    isCollect = true;
                    ToastUtils.showShort(getContext().getString(R.string.collect_suc));
                }
                isCollect();
                break;
            case R.id.tvRename:
                dismiss();
                if (listener != null) {
                    listener.clickRename();
                }
                break;
            case R.id.tvShare:
                dismiss();
                if (listener != null) {
                    listener.clickShare();
                }
                break;
            case R.id.tvFolder:
                dismiss();
                if (listener != null) {
                    listener.clickAddPrivacyFolder();
                }
                break;
            case R.id.tvInfo:
                dismiss();
                if (listener != null) {
                    listener.clickInfo();
                }
                break;
            case R.id.tvDelete:
                dismiss();
                if (listener != null) {
                    listener.clickDelete();
                }
                break;
            default:
                break;
        }
    }

    private void isCollect() {
        if (isCollect) {
            mDataBinding.ivVideoCollect.setImageResource(R.drawable.ayishoucang);
        } else {
            mDataBinding.ivVideoCollect.setImageResource(R.drawable.ashoucang);
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickRename();

        void clickShare();

        void clickAddPrivacyFolder();

        void clickInfo();

        void clickDelete();
    }
}
